package com.xyz.tasks.schedules;

import com.free.exception.SystemException;
import com.free.ext.logger.FreeLogger;
import com.free.ext.logger.FreeLoggerFactory;
import com.free.ext.quartz.TaskContainer;
import com.free.ext.quartz.tasker.MethodTasker;
import com.free.ext.quartz.tasker.RemoteUrlTasker;
import com.free.ext.quartz.tasker.Tasker;
import com.free.ext.spring.context.AppContext;
import com.free.utils.DateUtils;
import com.xyz.domain.table.SLottery;
import com.xyz.service.LotteryInfoService;
import com.xyz.tasks.bf.TaskSettingBf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Component
public class YldtTaskCenter {

    private static final FreeLogger LOG = FreeLoggerFactory.getLogger(YldtTaskCenter.class);

    @Autowired
    TaskSettingBf taskSettingBf;

    @Autowired
    LotteryInfoService lotteryInfoService;

    private ExecutorService executorService = Executors.newFixedThreadPool(5);

    public void loadTasks(){
        List<SLottery> lotteries = lotteryInfoService.getAllLotteryInfos().stream().filter(x->{
           return x.getType() == 1;
        }).collect(Collectors.toList());

        if(lotteries != null && lotteries.size() > 0){
            lotteries.stream().forEach(x->{
                executorService.execute(()->{
                    try {
                        taskSettingBf.mkTaskerInfo(x);
                    } catch (SystemException e) {
                        LOG.info("YldtTaskCenter,导入任务{}时发生异常{}",x.getCode(), e);
                    }
                });
            });
        }
    }

    public static void main(String[] args) {
//        Date currentDate = new Date();
//        String dateTimeStr = DateUtils.convert2String(currentDate, DateUtils.FormatEnum.yyyy_MM_dd) + " 00:00:00";
//        try {
//            Date dateTime = DateUtils.convert2Date(dateTimeStr, DateUtils.FormatEnum.yyyy_MM_dd_HH_mm_ss);
//            if(dateTime.compareTo(currentDate) <= 0){
//
//            }
//            long seconds = DateUtils.getDiffTimeOfSeconds(dateTime,currentDate);
//            int left = (int)seconds % 90;
//            System.out.println(seconds);
//            System.out.println(left);
//        } catch (SystemException e) {
//            e.printStackTrace();
//        }
        SLottery lottery = new SLottery();
        lottery.setDayStartTime("23:17");
        //mkLotteryTask(lottery);
    }

}
